package org.vectrics.recruiter.struts.tag;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ecs.html.A;
import org.apache.log4j.Category;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.company.CompanyPerson;
import org.vectrics.company.CompanyPersonRole;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.PageListTag;


public class CompanyPersonRoleListTag extends PageListTag  {

	private static final long serialVersionUID = 3851596555890600437L;
	
	private static Category log = Category.getInstance(CompanyPersonRoleListTag.class);
	private String companyPersonId = null;
    
    
	public CompanyPersonRoleListTag() {
		super();
	}
	
	public String getDescription() {
		return(null);
	}
	
	public String getNoElementsMessage() {
		return(this.getPhrase("phrase.no.company.person.roles"));
	}
	/**
	 * @return Returns the companyPersonId.
	 */
	public String getCompanyPersonId() {
		return companyPersonId;
	}
	/**
	 * @param companyPersonId The companyPersonId to set.
	 */
	public void setCompanyPersonId(String companyPersonId) {
		this.companyPersonId = companyPersonId;
	}
	protected CompanyPerson getCompanyPerson() {
		CompanyPerson companyPerson = CoreServiceLocator.getCompanyService().findCompanyPerson(new Long(this.getCompanyPersonId()));
		return(companyPerson);
	}
	
	protected PageList createPageList() {
		PageList pageList = null;
		pageList = new CompanyPersonRolePageList(getCompanyPerson());
		return(pageList);	
	}
	
    protected void makeHeaderCells(Row row) {
        log.debug("makeHeaderCells() called.");
        row.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");
        
        Cell cell = row.addCell();
        //cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("phrase.company.person.title")));

        cell = row.addCell();
		//cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("phrase.company.person.date.start")));

        cell = row.addCell();
		//cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("phrase.company.person.date.end")));

        cell = row.addCell();
        cell.addElement(TextHelper.getFont("tableHdrFont", "&nbsp;"));
		//cell.setClass("tableHdrBg");

    }
    
	
    
    protected void showList(String description, Cell mainCell, List roleList, Form form)
    {
        BorderTable table = new BorderTable();
        
        table.setID("border_table");
        table.setWidth(600);
        mainCell.addElement(table);
        makeHeaderCells(table.addRow());
        log.debug("showList() called.");
        Iterator iter = roleList.iterator();
        while (iter.hasNext()) {
        	CompanyPersonRole role = (CompanyPersonRole)iter.next();
        	makeObjectRow(table.addRow(), role);
        }
    }
	
    protected void makeObjectRow(Row row, CompanyPersonRole role) {
    	log.debug("makeObjectRow()");
    	
        Cell cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", role.getTitle()));

        cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale()).format(role.getDateStart())));

        cell = row.addCell();
        if (role.getDateEnd() != null) {
        	cell.addElement(TextHelper.getFont("subFont", CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale()).format(role.getDateEnd())));
        } else {
        	cell.addElement(TextHelper.getFont("subFont", this.getPhrase("phrase.company.person.date.present")));
        }
        
        cell = row.addCell();
        A link = new A();
        link.setHref(this.getContextPath() 
                + "/admin/company/person/role/edit.do?companyId=" + this.getCompanyPerson().getCompany().getId()
                + "&personId=" + this.getCompanyPerson().getPerson().getId() 
                + "&companyPersonRoleId=" + role.getId());
        link.addElement(this.getPhrase("button.edit"));
        link.setClass("subFont");
        cell.addElement(link);

    }
    
    public String getPageListLink() {
        return(getContextPath() + "/admin/company/index.do");
    }
    
    
    protected String getNewCompanyLink(HttpServletRequest request) {
        return(request.getContextPath() + "/admin/company/edit.do");
    }

    protected void setTableProperties(BorderTable table) {
        table.setWidth(500);
    }

    

}